package com.quasar.hpatient.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.quasar.hpatient.MainAppliaction;
import com.quasar.hpatient.R;
import com.quasar.hpatient.model.httpmodel.AnotherResult;
import com.quasar.hpatient.model.httpmodel.DataStateResult;
import com.quasar.hpatient.model.litePalmodel.TroubleInfodb;
import com.quasar.hpatient.model.medicalmodel.DiscomfortData;
import com.quasar.hpatient.onInterface.OnDataResultListener;
import com.quasar.hpatient.presenter.DiscomfortPresenter;
import com.quasar.hpatient.utils.LogAndToastUtil;
import com.quasar.hpatient.utils.Spanny;
import com.vise.log.ViseLog;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by 查询单日不适症状信息
 */
public class WidgetSymptoms extends LinearLayout implements View.OnClickListener, OnDataResultListener {


    private Context context;
    //不适症状详情描述
    DiscomfortData discomfortData;
    //不适症状
    private DiscomfortPresenter discomfortPresenter;
    List<DiscomfortData> discomfortDatas = new ArrayList<>();

    public WidgetSymptoms(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
        this.context = context;

    }

    public void setDate(String calendarDay) {
        discomfortPresenter = new DiscomfortPresenter(this);
        discomfortPresenter.GetRecentTroubleData(calendarDay);

    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.symptom_describe_layout, this);
        tv_symptom = (TextView) findViewById(R.id.tv_symptom);
        tv_look = (TextView) findViewById(R.id.tv_look);
        ll_layout = (LinearLayout) findViewById(R.id.ll_layout);
        ll_discomfort = (LinearLayout) findViewById(R.id.ll_discomfort);
        tv_look.setOnClickListener(this);

    }


    TextView tv_look, tv_symptom;//查看不适详情
    LinearLayout ll_layout;
    LinearLayout ll_discomfort;

    public WidgetSymptoms(Context context) {
        super(context);
        init(context);
    }

    private int color(int value) {
        return getContext().getResources().getColor(value);
    }

    @Override
    public void onClick(View v) {
        Spanny spanny = new Spanny();
        //不适症状信息描述
        if (v.getId() == R.id.tv_look) {
            //不适描述信息
            if (discomfortDatas != null) {
                int num = discomfortDatas.size();
                if (num != 0) {
                    for (int i = 0; i < num; i++) {
                        if(i==0){
                            spanny.append("\n"+discomfortDatas.get(i).getLogTime().substring(0, discomfortDatas.get(i).getLogTime().length() - 3)+"\n", new ForegroundColorSpan(color(R.color.textcolor)));
                        }
                        spanny.append("\n不适症状:", new ForegroundColorSpan(color(R.color.btcolor)));
                        spanny.append(discomfortDatas.get(i).getTroubleName() + "\n", new ForegroundColorSpan(color(R.color.textcolor)));

                        if (discomfortDatas.get(i).getDurationLength() != null  && discomfortDatas.get(i).getDurationLength().length()>0 ) {
                            spanny.append("时长:" ,new ForegroundColorSpan(color(R.color.btcolor)));
                            spanny.append("\t" + discomfortDatas.get(i).getDurationLength()+"\n", new ForegroundColorSpan(color(R.color.textcolor)));
                        }

                        if(discomfortDatas.get(i).getDescription()!=null && discomfortDatas.get(i).getDescription().length()>0){
                            spanny.append("描述:", new ForegroundColorSpan(color(R.color.btcolor)));
                            spanny.append("\t" + discomfortDatas.get(i).getDescription()+"\n", new ForegroundColorSpan(color(R.color.textcolor)));
                        }

                    }
                }
                AlertDialog.Builder dialog = new AlertDialog.Builder(context);
                dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).setMessage(spanny).show();

            }
        }
    }

    @Override
    public void onViewFirstSuccess(AnotherResult<DiscomfortData> data) {

    }

    //不适时长
    String TimeDate = null;

    @Override
    public void onViewNextSuccess(AnotherResult<DiscomfortData> data) {
        discomfortDatas.clear();

        Spanny spannys = new Spanny();

        if (data.getList() == null || data.getList().size() == 0) {
            ll_discomfort.setVisibility(View.GONE);
        } else {
            //保存不适症状信息
            discomfortDatas.addAll(data.getList());

            ll_discomfort.setVisibility(View.VISIBLE);
            int size = data.getList().size();
            if (size > 0) {
                discomfortData = data.getList().get(size - 1);

                if (discomfortData.getLogTime() != null && discomfortData.getLogTime().length() > 0) {
                    spannys.append(discomfortData.getLogTime().substring(0, discomfortData.getLogTime().length() - 3) + "\n\n");
                }
                spannys.append("事件:");
                spannys.append(discomfortData.getTroubleName(), new ForegroundColorSpan(color(R.color.red)));

                //不适时长
                spannys.append("\n");
                spannys.append(discomfortData.getDurationLength(), new ForegroundColorSpan(color(R.color.red)));
                TimeDate = discomfortData.getTroubleName();
            }

            tv_symptom.setText(spannys);
        }
    }

    @Override
    public void onViewError(String msg) {
        if (msg != null) {
            LogAndToastUtil.toast(context, msg);
            ViseLog.d("事件显示" + msg);
        }
    }

    @Override
    public void onViewRefreshUI() {

    }

    @Override
    public void onSubSymptoms(DataStateResult dataStateResult) {

    }

    @Override
    public void onViewRefreshListSuccess() {

    }

    @Override
    public void onUpdateDescribe(DataStateResult dataStateResult) {

    }
}
